﻿package mortal.game.view.copy.groupCopy.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.GroupTowerCopy.data.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.game.view.copy.groupCopy.item.*;
    import mortal.mvc.core.*;

    public class GroupCopyGroupDefensePanel extends GroupCopyBasePanel
    {
        protected var _leftBg:GImageBitmap;
        protected var _rightBg:ScaleBitmap;
        protected var _leftBottomBg:ScaleBitmap;
        protected var _leftTitleLine:GBitmap;
        protected var _rightTitleLine:GBitmap;
        protected var _timesDes:GImageBitmap;
        protected var _div:GImageBitmap;
        protected var _timeBack:GImageBitmap;
        protected var _fTime:BitmapNumberText;
        protected var _totalTime:BitmapNumberText;
        protected var _rewardTitle:GBitmap;
        protected var _levelNum:BitmapNumberText;
        protected var _groupTitleBg:GBitmap;
        protected var _listTitleBg:ScaleBitmap;
        private var _copyList:GTileList;
        protected var _boReward:GroupCopyDefenceBossRewardPanel;
        private var _dicIndexClass:Dictionary;
        private var _currentPanel:GroupCopyBaseListPanel;
        private var _rewardList:GTileList;
        private var _tcopy:TCopy;
        private var _resNum:int = 0;
        public static const GroupDefenCopyId:int = 138;

        public function GroupCopyGroupDefensePanel()
        {
            this._dicIndexClass = new Dictionary();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.groupCopy, this.onResLoad);
            LoaderHelp.addResCallBack(ResFileConst.groupCopyDefense, this.onResLoad);
            return;
        }// end function

        private function onResLoad() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this._resNum + 1;
            _loc_1._resNum = _loc_2;
            if (this._resNum < 2)
            {
                return;
            }
            this._leftBg = UIFactory.gImageBitmap(ImagesConst.GroupCopyGroupDefenseBg, 8, 5, this);
            this._rightBg = UIFactory.bg(425, 4, 470, 549, this, ImagesConst.PanelBg);
            this._leftBottomBg = UIFactory.bg(6, 413, 418, 140, this, ImagesConst.PanelBg);
            this._leftTitleLine = UIFactory.gBitmap(ImagesConst.TitleLine, 492, 25, this);
            this._rightTitleLine = UIFactory.gBitmap(ImagesConst.TitleLine, 815, 25, this);
            this._rightTitleLine.scaleX = -1;
            this._timesDes = UIFactory.gImageBitmap(ImagesConst.GroupCopyTxt_1, 530, 231, this);
            this._div = UIFactory.gImageBitmap(ImagesConst.GroupCopyTxt_2, 702, 227, this);
            this._timeBack = UIFactory.gImageBitmap(ImagesConst.GroupCopyTxt_3, 741, 231, this);
            this._fTime = UIFactory.bitmapNumberText(680, 228, "GroupCopyNum.png", 31, 35, -16, this, 13, 0);
            this._fTime.text = "1";
            this._totalTime = UIFactory.bitmapNumberText(716, 228, "GroupCopyNum.png", 31, 35, -16, this, 13, 0);
            this._totalTime.text = "1";
            this._rewardTitle = UIFactory.gBitmap(ImagesConst.GroupCopyRewardTitle, 273, 320, this);
            this._levelNum = UIFactory.bitmapNumberText(303, 318, "DailyNormalNum.png", 19, 21, -10, this, 9, 1);
            this._levelNum.text = "100";
            this._groupTitleBg = UIFactory.gBitmap(ImagesConst.GroupCopyGroupDefenTitle, 591, 19, this);
            this._listTitleBg = UIFactory.bg(423, 259, 470, 23, this, ImagesConst.RegionTitleBg);
            this._copyList = UIFactory.tileList(428, 40, 475, 190, this);
            this._copyList.direction = GBoxDirection.VERTICAL;
            this._copyList.verticalGap = -5;
            this._copyList.horizontalGap = -6;
            this._copyList.rowHeight = 101;
            this._copyList.columnWidth = 236;
            this._copyList.columnCount = 2;
            this._copyList.rowCount = 2;
            this._copyList.width = 475;
            this._copyList.setStyle("cellRenderer", GroupDefenseCopyCell);
            this._copyList.configEventListener(Event.CHANGE, this.handleCellRendererChange);
            this._copyList.selectedIndex = 0;
            this._rewardList = UIFactory.tileList(240, 349, 500, 60, this);
            this._rewardList.columnWidth = 42;
            this._rewardList.rowHeight = 60;
            this._rewardList.horizontalGap = 0;
            this._rewardList.verticalGap = 0;
            this._rewardList.setStyle("cellRenderer", GroupCopyRewardCellRender2);
            this._boReward = UIFactory.getUICompoment(GroupCopyDefenceBossRewardPanel, 6, 415, this);
            this.initPanelClass();
            this.updateCopyList();
            this.updateLimitNum();
            this.showMeetCarrarOnly();
            this.selectRwCopy();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._resNum = 0;
            LoaderHelp.removeResEvent(ResFileConst.groupCopy, this.onResLoad);
            LoaderHelp.removeResEvent(ResFileConst.groupCopyDefense, this.onResLoad);
            if (this._leftBg)
            {
                this._leftBg.scaleX = 1;
            }
            this._leftBg = null;
            this._rightBg = null;
            this._leftBottomBg = null;
            this._leftTitleLine = null;
            this._rightTitleLine = null;
            this._timesDes = null;
            this._div = null;
            this._timeBack = null;
            this._fTime = null;
            this._totalTime = null;
            this._rewardTitle = null;
            this._groupTitleBg = null;
            this._listTitleBg = null;
            this._levelNum = null;
            this._dicIndexClass = new Dictionary();
            this._currentPanel = null;
            this._rewardList = null;
            this._boReward = null;
            this._copyList = null;
            return;
        }// end function

        private function initPanelClass() : void
        {
            this._dicIndexClass[0] = GroupCopyListPanel;
            this._dicIndexClass[1] = GroupCopyMateInfoPanel;
            return;
        }// end function

        private function selectRwCopy() : void
        {
            if (GroupCopyWaitingView.openCopy)
            {
                return;
            }
            var _loc_1:* = GroupCopyUtil.checkRewByIndex(1);
            if (_loc_1 && _loc_1.length > 1)
            {
                this._copyList.selectedIndex = _loc_1[1];
                this.handleCellRendererChange();
            }
            return;
        }// end function

        private function showMeetCarrarOnly() : void
        {
            if (this._tcopy == null)
            {
                this._tcopy = CopyConfig.instance.getCopyInfoByCode(GroupDefenCopyId);
            }
            var _loc_1:* = this._tcopy.rewardShow.split("#");
            this._rewardList.dataProvider = this.getRewardDataProvider(_loc_1);
            return;
        }// end function

        private function getRewardDataProvider(param1:Array) : DataProvider
        {
            var _loc_3:Object = null;
            var _loc_4:ItemData = null;
            var _loc_6:int = 0;
            var _loc_2:* = new DataProvider();
            var _loc_5:* = param1.length;
            while (_loc_6 < _loc_5)
            {
                
                if (param1[_loc_6])
                {
                    _loc_4 = new ItemData(int(param1[_loc_6]));
                    _loc_3 = {itemData:_loc_4};
                    if (_loc_4.itemInfo && ItemsUtil.isFitCarrer(_loc_4))
                    {
                        _loc_3 = {itemData:_loc_4};
                        _loc_2.addItem(_loc_3);
                    }
                }
                _loc_6++;
            }
            return _loc_2;
        }// end function

        public function updateGroupList(param1:SCopyWaitingGroupMsg) : void
        {
            if (this._currentPanel is GroupCopyListPanel)
            {
                (this._currentPanel as GroupCopyListPanel).updateGroupList(param1);
            }
            return;
        }// end function

        public function updateGroupMateList() : void
        {
            if (Cache.instance.group.isInCopyGroup)
            {
                if (this._currentPanel is GroupCopyMateInfoPanel)
                {
                    if (this._tcopy.code == Cache.instance.group.sgroup.copyCode)
                    {
                        (this._currentPanel as GroupCopyMateInfoPanel).updateGroupMateList();
                    }
                    else
                    {
                        this.changeCopyGroupInfoByIndex(0);
                    }
                }
                else if (this._currentPanel is GroupCopyListPanel && this._tcopy.code == Cache.instance.group.sgroup.copyCode)
                {
                    this.changeCopyGroupInfoByIndex(1);
                }
            }
            else if (this._currentPanel is GroupCopyMateInfoPanel)
            {
                this.changeCopyGroupInfoByIndex(0);
            }
            return;
        }// end function

        private function handleCellRendererChange(event:Event = null) : void
        {
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            if ((this._copyList.selectedItem.tCopy as TCopy).enterMinLevel > _loc_2 && _loc_2 <= (this._copyList.selectedItem.tCopy as TCopy).enterMaxLevel)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(70885, (this._copyList.selectedItem.tCopy as TCopy).enterMinLevel));
            }
            this._tcopy = this._copyList.selectedItem.tCopy;
            var _loc_3:* = this._tcopy.rewardShow ? (this._tcopy.rewardShow) : ("");
            var _loc_4:* = this._tcopy.rewardShow.split("#");
            this._rewardList.dataProvider = this.getRewardDataProvider(_loc_4);
            this._rewardList.x = 240 + (170 - 42 * this._rewardList.dataProvider.length) * 0.5;
            this._levelNum.text = "" + this._tcopy.enterMinLevel;
            this.updateBoReward(this._tcopy.code);
            this._leftBg.imgUrl = "defenseSingleBig_" + this._tcopy.enterMinLevel + ".jpg";
            if (Cache.instance.group.isInCopyGroup && this._tcopy.code == Cache.instance.group.sgroup.copyCode)
            {
                if (!(this._currentPanel is GroupCopyMateInfoPanel))
                {
                    this.changeCopyGroupInfoByIndex(1);
                }
            }
            else
            {
                this.changeCopyGroupInfoByIndex(0);
            }
            return;
        }// end function

        private function getCopyDataProvider() : DataProvider
        {
            var _loc_3:Object = null;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = CopyConfig.instance.getDefenseCopy(Cache.instance.role.roleInfo.level, DefenseInfo.TeamDefenseCopyCode);
            var _loc_4:* = 0 * 4;
            var _loc_5:* = _loc_2.length;
            var _loc_6:* = _loc_4;
            while (_loc_6 < _loc_5)
            {
                
                _loc_3 = {tCopy:_loc_2[_loc_6]};
                _loc_1.addItem(_loc_3);
                _loc_6++;
            }
            return _loc_1;
        }// end function

        public function changeCopyGroupInfoByIndex(param1:int) : void
        {
            var _loc_2:GroupCopyBaseListPanel = null;
            if (!(this._currentPanel is this._dicIndexClass[param1]))
            {
                _loc_2 = UICompomentPool.getUICompoment(this._dicIndexClass[param1]);
                if (this._currentPanel)
                {
                    this._currentPanel.dispose();
                }
                this.addChild(_loc_2);
                this._currentPanel = _loc_2;
                this._currentPanel.x = 400;
                this._currentPanel.y = 220;
            }
            this._currentPanel.tcopy = this._tcopy;
            if (this._currentPanel is GroupCopyListPanel)
            {
            }
            else if (this._currentPanel is GroupCopyMateInfoPanel)
            {
                (this._currentPanel as GroupCopyMateInfoPanel).updateGroupMateList();
            }
            return;
        }// end function

        public function updateCopyList() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Boolean = false;
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            this._copyList.dataProvider = this.getCopyDataProvider();
            if (Cache.instance.group.isInCopyGroup)
            {
                _loc_1 = this._copyList.dataProvider.length;
                _loc_2 = false;
                while (_loc_3 < _loc_1)
                {
                    
                    _loc_4 = this._copyList.dataProvider.getItemAt(_loc_3);
                    if (_loc_4.tCopy.code == Cache.instance.group.sgroup.copyCode)
                    {
                        this._copyList.selectedIndex = _loc_3;
                        _loc_2 = true;
                        break;
                    }
                    _loc_3++;
                }
                if (_loc_2 == false)
                {
                    this._copyList.selectedIndex = 0;
                }
            }
            else
            {
                this._copyList.selectedIndex = 0;
            }
            this.handleCellRendererChange();
            return;
        }// end function

        public function updateLimitNum() : void
        {
            this._fTime.text = "" + Cache.instance.copy.getGroupCopyEnterNumByGroup(GroupDefenCopyId);
            this._totalTime.text = "" + GroupTowerCopyUtil.getTotalNumByGroup(GroupDefenCopyId);
            return;
        }// end function

        private function updateBoReward(param1:int = 0) : void
        {
            param1 = param1 == 0 ? (DefenseInfo.TeamDefenseCopyCode) : (param1);
            this._boReward.initReward(param1, 3);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyGetBoGetRwInfo, param1));
            return;
        }// end function

        public function updateBoRwAfterGet() : void
        {
            if (this._boReward)
            {
                this._boReward.updateListAfterGet();
            }
            return;
        }// end function

    }
}
